from datetime import datetime

from pytils.translit import slugify

from app.app import db


class News(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(255), unique=True)
    content = db.Column(db.Text)
    slug = db.Column(db.String(255))
    date_of_creation = db.Column(db.DateTime, default=datetime.now())
    date_of_update = db.Column(db.DateTime)

    def __init__(self, *args, **kwargs):
        super(News, self).__init__(*args, **kwargs)
        self.slug = slugify(self.title)
        self.date_of_update = datetime.now()

    def __repr__(self):
        return self.title
